// pages/home_details/home_details.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    title: null,
    timePrice: null,
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    imgs: [],
    indicatorDots: true,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    latitude: "23.1308",
    longitude: "113.401962",
    isAuthentication: false, // 字段是否实名认证
    phoneNumber: "123456789012", //电话号码
    isCollect: true, //是否收藏
    publishState: 1, //展示状态 0 下架 1上架
    src: "",//来自哪个页面
    markers: [{
      iconPath: "../../images/position_marker.png",
      id: 0,
      latitude: 23.1308,
      longitude: 113.401962,
      width: 32,
      height: 32
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (app.globalData.homeDetail) {
      // console.log("home_details onload: ", options, app.globalData.homeDetail);
      let homeDetail = app.globalData.homeDetail.attributes;
      var tp = JSON.parse(homeDetail.timePrice);
      this.setData({
        detail: app.globalData.homeDetail.attributes,
        imgs: homeDetail.images,
        timePrice: tp,
      });

      var that = this
      var src = options.src
      //将接收到的字符串转化成json对象（这里就和后台传输的数据处理方式一样）是数组的话用这种方式接收
      that.setData({
        src: src
        //单个的数据就直接拿来用
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  onCollect: function(e) {
    this.setData({
      isCollect: !this.data.isCollect,
    });
    wx.showToast({
      title: 'onCollect',
    });
  },

  onContact: function(e) {
    if (this.data.phoneNumber.length > 0) {
      wx.makePhoneCall({
        phoneNumber: this.data.phoneNumber,
      })
    }
  },

  doPublish: function(e) {
    var that = this
    if (this.data.publishState == 0) {
      wx.showModal({
        title: '提示',
        content: '是否确定上架？上架后其他用户可以看到你发布的本条信息',
        success: function(res) {
          if (res.confirm) {
            //执行上架工作
            that.setData({
              publishState: 1,
            });
            //console.log('用户点击确定')
          } else {
            //console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '是否确定下架？下架后其他用户将看不到你发布的本条信息',
        success: function(res) {
          if (res.confirm) {
            //执行上架工作
            that.setData({
              publishState: 0,
            });
            //console.log('用户点击确定')
          } else {
            //console.log('用户点击取消')
          }
        }
      })
    }
  },

  doEdit: function(e) {
    //跳转到发布页面后 读取本地数据 让已填写的数据显示出来
    wx.navigateTo({
      url: '../fillin/fillin?src=home_details',
    })
  },
})